import Vue from 'vue';
import Router from 'vue-router';
import {api_url, ERR_OK} from '@/config/config';
import store from '../store'

if (process.env.NODE_ENV === 'development') {
  Vue.use(Router);
}
// Vue.use(Router);


// 子页面用‘_’分隔，第一个为首页
const pages = {
  Home: resolve => require.ensure([], () => resolve(require('@/pages/center/home')), 'center'),
  EditUserInfo: resolve => require.ensure([], () => resolve(require('@/pages/center/edituserinfo')), 'center'),
  BindPhone: resolve => require.ensure([], () => resolve(require('@/pages/center/bindphone')), 'center'),
  Config: resolve => require.ensure([], () => resolve(require('@/pages/center/config')), 'center'),
  Rate: resolve => require.ensure([], () => resolve(require('@/pages/center/rate')), 'center'),
  Push: resolve => require.ensure([], () => resolve(require('@/pages/center/push')), 'center'),
  PushAdd:resolve => require.ensure([], () => resolve(require('@/pages/center/pushAdd')), 'center'),
  
  Auth: resolve => require.ensure([], () => resolve(require('@/pages/login/auth')), 'auth'),
  Pass: resolve => require.ensure([], () => resolve(require('@/pages/login/pass')), 'pass'),
  UnlockSucc: resolve => require.ensure([], () => resolve(require('@/pages/login/UnlockSucc')), 'UnlockSucc'),

  Binding: resolve => require.ensure([], () => resolve(require('@/pages/bind/binding')), 'bind'),

  SetNetwork: resolve => require.ensure([], () => resolve(require('@/pages/internet/setnetwork')), 'internet'),
  CreateWM: resolve => require.ensure([], () => resolve(require('@/pages/internet/creatEWM')), 'internet'),

};

// 后台子路由
// pages.Invite.children = {
// Down // /invite/down
// };

// 配置路由
const setRoutes = function (routeItems, path = '', defaultRoute, callback) {
  if (!routeItems) return [];
  let res = [],
    first = true;
  defaultRoute = defaultRoute || path || '/';
  for (let key in routeItems) {
    let route = {
      path: path + '/' + key.toLocaleLowerCase().replace(/_/ig, '/'),
      name: key.replace(/_/ig, ''),
      meta: {},
      component: routeItems[key]
    };
    route.children = setRoutes(routeItems[key].children, route.path, '', callback);
    // 默认路由
    first && (route.alias = defaultRoute);
    first = false;
    typeof callback === 'function' && callback(route);
    res.push(route);
  }
  res.push({path: '**', redirect: defaultRoute});
  return res;
};


let routes = setRoutes(pages, '', '', item => {
  // 是否需要登录
  // item.meta.auth = true;

  // 处理特殊组件
  switch (item.name) {
    case 'Home':
      item.meta.title = '个人中心';
      item.meta.bgc = '#f5f5f5';
      break;
    case 'EditUserInfo':
      item.meta.title = '个人中心';
      item.meta.bgc = '#f5f5f5';
      item.path += '/:id';
      break;
    case 'BindPhone':
      item.meta.title = '手机绑定';
      break;
    case 'Config':
      item.meta.title = '防沉迷设置';
      break;
    case 'Rate':
      item.meta.title = '学习进度';
      break;
    case 'Binding':
      item.path += '/:type';
      break;
    case 'SetNetwork':
    case 'CreateWM':
      item.meta.title = 'WIFI';
      break;
    case 'PushAdd':
      item.meta.title = '新增';
      item.meta.bgc = '#f5f5f5';
      break;
  }
 
});

const router = new Router({
  // mode: 'history',
  routes,
  scrollBehavior(to, from, savedPosition) {
    return {x: 0, y: 0}
  }
});
// console.log(routes);

let ua = navigator.userAgent.toLowerCase();
let regNavigator = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;


// 使用假的微信用户信息
function testWxInfo(uid = '', params = {}) {
  let user = {
    uid,
    is_register: !!uid
  };
  let res = {
    "nickname": "👻",
    "realname": "\\ud83d\\udc7b",
    "openid": "oTwpBwAnwIqMj9SzlFSiBA4F9I3A",
    "unionid": "oTwpBwAnwIqMj9SzlFSiBA4F9ttt",
    "icon": "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83erF0K5Ue2DicWicXZbDVJrPFb0jqyK9NX9ibEG7KVP9eKmCLib5ibeibgrNswZKg8iaS3mP4f2zIHYtKSvsQ/132",
    "uid": "",
    "is_register": false,
    "content": "{\"openid\":\"oTwpBwAnwIqMj9SzlFSiBA4F9I3A\",\"nickname\":\"\\\\ud83d\\\\udc7b\",\"sex\":1,\"language\":\"zh_CN\",\"city\":\"Wuzhou\",\"province\":\"Guangxi\",\"country\":\"CN\",\"headimgurl\":\"http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83erF0K5Ue2DicWicXZbDVJrPFb0jqyK9NX9ibEG7KVP9eKmCLib5ibeibgrNswZKg8iaS3mP4f2zIHYtKSvsQ/132\",\"privilege\":[]}"
  };
  params = res;
  // params.unionid = res.unionid;
  store.commit('setWxUserInfo', Object.assign(params, user));
  if (user.uid) {
    store.commit('setUserInfo', {uid: user.uid});
  }
}

/*  */

router.beforeEach((to, from, next) => {
  try {
    testWxInfo();
  } catch (e) {
  }

  if (to.meta.auth && !store.state.userInfo.uid) {

  }
  let app = document.body;
  app.style.backgroundColor = to.meta.bgc || '#fff';
  document.title = to.meta.title || '家长助手';
  next();
});

export default router;
